/*
 * $Id$
 */
package py.windmill.web;

import py.windmill.action.ActionSupport;
import py.windmill.action.Request;
import py.windmill.action.Resolution;

import py.windmill.resolution.TilesSupport;

import static org.apache.commons.lang3.StringUtils.*;
import py.windmill.web.article.Articles;

/**
 *
 * @author myang
 */
public class ReadingAction extends ActionSupport {

    @Override
    public Resolution onGet(final Request req) throws Exception {
        final String yyyyMM = req.param("yyyyMM");
        final String name = req.param("name");
        String page = "reading/months.jsp";
        if (isEmpty(name)) {
            if (isEmpty(yyyyMM)) {
                req.attr("months", Articles.months());
            } 
            else {
                page = "reading/articles.jsp";
                req.attr("articles", Articles.get(yyyyMM));
            }
        } 
        else {
            req.attr("article", Articles.read(yyyyMM, name));
            page = "reading/article.jsp";
        }
        return new TilesSupport("page", page);
    }

    @Override
    public String boundTo() {
        return "/reading/{yyyyMM}/{name}";
    }
}
